﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text;
using MVCLD.Helpers;

namespace MVCLD.Models
{
    public class ArticleModels
    {
        #region Data Access
        MVCLDServiceModels db = new MVCLDServiceModels();
        #endregion

        /// <summary>
        /// Check the article name is not already in DB
        /// </summary>
        /// <param name="ArticleTitle">The title of the article</param>
        /// <returns>True/False</returns>
        public Boolean DoesArticleNameExist(string ArticleTitle)
        {
                var art = db.GetSingleArticle(ArticleTitle);
            try
            {
                if (String.IsNullOrEmpty(art.ArticleTitle))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {

                return false;
            }
        }

        public Article formatArticle(Article article) 
        {
            article.ArticleContent = ArticleBodyText(article.ArticleContent);
            article.Resource = ArticleResourceBox(article.Resource);
            return article;
        }

        public string ArticleBodyText(string articletext) 
        { 
            //first replace double line breaks
            articletext = "<p>" + articletext.Replace("\r\n\r\n", "</p><p>") + "</p>";
            
            // Now replace any sinle ones left to just a br
            articletext = articletext.Replace("\r\n", "<br />");

            return articletext;
        }

        public string ArticleResourceBox(string resource)
        {
            // convert the BB code
            resource = Helpers.Helpers.CovertBBCode(resource);
            return resource;
        }

        public string PayPalFormCode(Article art, ArticleType artType, string Currency) 
        {
            StringBuilder s = new StringBuilder();
            s.Append("<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">");
            s.Append("<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">");
            s.Append("<input type=\"hidden\" name=\"business\" value=\"" + Settings.Settings.PayPalEmailAddress + "\">");
            s.Append("<input type=\"hidden\" name=\"item_name\" value=\"Article Submission - " + art.ArticleTitle + "\">");
            s.Append("<input type=\"hidden\" name=\"amount\" value=\"" + String.Format("{0:F}", artType.Cost) + "\">");
            s.Append("<input type=\"hidden\" name=\"currency_code\" value=\"" + Currency + "\">");
            s.Append("<input type=\"hidden\" name=\"button_subtype\" value=\"services\">");
            s.Append("<input type=\"hidden\" name=\"bn\" value=\"PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest\">");
            s.Append("<input type=\"image\" src=\"https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif\" border=\"0\" name=\"submit\" alt=\"PayPal - The safer, easier way to pay online!\">");
            s.Append("<img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">");
            s.Append("</form>");
            return s.ToString();
        }

    }

    // Validation

    [MetadataType(typeof(Article_Validation))]
    public partial class Article
    {
        class Article_Validation
        {
            [ScaffoldColumn(false)]
            public Guid ArticleID { get; set; }

            [Required(ErrorMessage = "Article title is required")]
            [StringLength(300)]
            [DisplayName("Article Title")]
            public string ArticleTitle { get; set; }

            [Required(ErrorMessage = "Submitter name is required")]
            [StringLength(300)]
            [DisplayName("Submitter Name")]
            public string SubmitterName { get; set; }

            [ScaffoldColumn(false)]
            public DateTime SubmissionDate { get; set; }

            [Required(ErrorMessage = "Article snippet required")]
            [DisplayName("Article Snippet")]
            public string ArticleSnippet { get; set; }

            [Required(ErrorMessage = "Article content is required")]
            [DisplayName("Article")]
            public string ArticleContent { get; set; }

            [DisplayName("Article Resources")]
            public string Resource { get; set; }

            [UIHint("CategoryDropDown")]
            [Required(ErrorMessage = "Please choose a category")]
            [DisplayName("Article Category")]
            public Guid CategoryID { get; set; }

            [ScaffoldColumn(false)]
            public string SubmitterIP { get; set; }

            [Required(ErrorMessage = "Email is required")]
            [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")]
            [DisplayName("Email Address")]
            public string Email { get; set; }

            [UIHint("LinkTypeDropDown")]
            [Required(ErrorMessage = "Please choose a article type")]
            [DisplayName("Article Submission Type")]
            public Guid ArticleTypeID { get; set; }

        }
    }

}